package com.darrenchan.leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-12-31 14:42
 */
public class Q207 {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        int[] nodesIndegree = getNodesIndegree(numCourses, prerequisites);
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < nodesIndegree.length; i++) {
            if (nodesIndegree[i] == 0) {
                queue.add(i);
            }
        }

        int count = 0;
        while (!queue.isEmpty()) {
            Integer node = queue.poll();
            count++;
            for (int i = 0; i < prerequisites.length; i++) {
                if (prerequisites[i][1] == node) { //邻居
                    nodesIndegree[prerequisites[i][0]]--;
                    if (nodesIndegree[prerequisites[i][0]] == 0) {
                        queue.add(prerequisites[i][0]);
                    }
                }
            }
        }

        return count == numCourses;
    }

    private int[] getNodesIndegree(int numCourses, int[][] prerequisites) {
        int[] nodesIndegree = new int[numCourses];
        for (int i = 0; i < prerequisites.length; i++) {
            nodesIndegree[prerequisites[i][0]]++;
        }
        return nodesIndegree;
    }
}
